
import { tablesInterface } from "@/common/interface/table.interface";
import TablePage from "@/components/TablePage/TablePage";
import { defineComponent, reactive, ref } from "vue";
import Add from "./Add/Add";
import songListService from "./index.service";

export default defineComponent({
  name: 'Index',
  setup() {
    const tablePageRef = ref(null as any);
    // 表格定义
    const tables = reactive({
      list: [
        {
          title: "歌曲列表",
          rowKey: "id",
          columns: [
            {
              title: "ID",
              dataIndex: "id",
              align: "center",
              width: "80px",
            },
            // {
            //   title: '所属分类',
            //   dataIndex: "cid",
            //   align: "center",
            //   width: "120px",
            // },
            {
              title: "中文名称",
              dataIndex: "chineseName",
              align: "center",
              width: "150px",
            },
            {
              title: "英文名称",
              dataIndex: "englishName",
              align: "center",
              width: "150px",
            },
            {
              title: "封面",
              dataIndex: "coverPic",
              align: "center",
              width: "200px",
            },
            {
              title: "曲谱类型标签",
              dataIndex: "museLabel",
              align: "center",
              width: "200px",
            },
            {
              title: "等级",
              dataIndex: "level",
              align: "center",
              width: "120px",
            },
            {
              title: "语言类型",
              dataIndex: "languageType",
              align: "center",
              width: "120px",
            },
            {
              title: "排序",
              dataIndex: "sort",
              align: "center",
              width: "120px",
            },
          ],
          toolbar: {
            left: ["refresh", "add"],
            right: ["filter"],
            add: {
              handler: () => {
                const addPopupRef = tablePageRef.value.getPopup("addRef");
                addPopupRef.value.open({ type: 'add' });
              }
            },
          },
          service: new songListService(),
          bodyCell: ({ text, record, index, column }) => {
            if (column.dataIndex === 'level') {
              switch (record.level) {
                case 1:
                  return <span>入门</span>
                case 3:
                  return <span>初级</span>
                case 5:
                  return <span>提高</span>
                case 7:
                  return <span>高级</span>
                default:
                  return <span>挑战</span>
              }
            }
            if (column.dataIndex === 'coverPic') {
              return <a-image src={record.coverPic}></a-image>
            }
            if (column.dataIndex === 'languageType') {
              return record.languageType === 0 ? <span>中文</span> : <span>英文</span>
            }
          },
          operation: [
            {
              type: "edit",
              clickHandler: ({ record }) => {
                const addPopupRef = tablePageRef.value.getPopup("addRef");
                addPopupRef.value.open({ type: 'edit', id: record.id });
              }
            },
            {
              type: "delete",
              clickHandler: "delete",
            },
          ],
          popups: [
            {
              ref: "addRef",
              dom: () => <Add onChange={() => { tablePageRef.value.reload() }} />
            }
          ],
          pagination: true,
          tableSourceFormat: {
            data: 'rows',
            pageSize: 'pageSize',
            current: 'pageNum',
          },
        }
      ],
    } as tablesInterface)
    // 配置项
    const config = reactive({
      table: tables.list[0],
    })
    return () => (
      <TablePage ref={tablePageRef} config={config}></TablePage>
    )
  }
})